/*
CONTROL CODE FOR THE DATE INPUT CONTROL
*/
function __PC_SetCalendarStyles( calname )
{
	var cal = document.getElementById( calname );
	if ( cal == null ) return;
	
	cal.style.display = "none";
	cal.style.position = "absolute";
	cal.style.border = "1px outset";
	cal.style.width = "250px";
	cal.style.backgroundColor = "silver";

	var gcMask = document.getElementById("_GlobalCalendarMask");
	gcMask.style.display = "none";

}
var CAL_MONTHS = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec");
var CAL_FULLMONTHS = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var CAL_DAYSINMONTH = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var CAL_DAYSINMONTHLY = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
var CAL_JUSTDATE=1;
var CAL_WITHTIME=2;

var _selected = new Date();
var _tempStyle = CAL_JUSTDATE;
var _tempDate = new Date();

var _calPopEl = null;
var _calPopInp = null;

function OffsetMonth( dt, months )
{
	var t_year = dt.getFullYear();
	var t_month = dt.getMonth()+1;
	var t_day = dt.getDate();
	
	t_month += months;
	if( (t_month > 12) || (t_month < 1))
	{
		// Need to adjust the year accordingly
		if( months > 0 )
		{
			while( t_month > 12 )
			{
				t_year++;
				t_month-=12;
			}
		}
		else
		{
			while( t_month < 1 )
			{
				t_year--;
				t_month+=12;
			}
		}
	}
	// Check the day is within the limits
	t_month--;
	if( ! __PC_ISLEAP( t_year ))
		if( t_day > CAL_DAYSINMONTH[t_month] ) t_day = CAL_DAYSINMONTH[t_month];
	else
		if( t_day > CAL_DAYSINMONTHLY[t_month] ) t_day = CAL_DAYSINMONTHLY[t_month];
	
	dt.setDate( t_day );
	dt.setMonth( t_month );
	dt.setYear( t_year );
	return dt;
}
function __PC_ISLEAP( yr )
{
	// Not a leap year by default
	var val = false;

	// Leap years are every 4 years
	if( (yr % 4)==0 ) val = true;
	// Except on the 100'th
	if( val && ((yr % 100)==0 ))
	{
		val = false;
		// Unless it's a 1000'th
		if( (yr % 1000)== 0 ) val = true;
	}
	return val;
}

function __PC_PrevMonth()
{
	_tempDate = OffsetMonth(_tempDate, -1);
	__PC_PopulateCalendar();
}
function __PC_NextMonth()
{
	tempDate = OffsetMonth(_tempDate, 1);
	__PC_PopulateCalendar();
}
function __PC_KeyDown()
{
	if( event.keyCode == 27 )
	{
		if( _calPopEl != null )
		{
			__PopdownCalendar(0);
		}
	}
}

function __CAL_FullOffsetX( ctrl )
{
	if( ctrl == null ) return 0;
	var offsetX = ctrl.offsetLeft;
	if( ctrl.offsetParent != null )
	{
		offsetX += __CAL_FullOffsetX( ctrl.offsetParent );
	}
	return offsetX;
}
function __CAL_FullOffsetY( ctrl )
{
	if( ctrl == null ) return 0;
	var offsetY = ctrl.offsetTop;
	if( ctrl.offsetParent != null )
	{
		offsetY += __CAL_FullOffsetY( ctrl.offsetParent );
	}
	return offsetY;
}

function __CAL_GetDateString( dt, format )
{
	var t_date = "" + dt.getDate();
	var t_month = CAL_MONTHS[dt.getMonth()];
	var t_year = "" + dt.getFullYear();
	var t_hour = "" + dt.getHours();
	var t_minutes = "" + dt.getMinutes();
	
	if( t_date.length < 2 ) t_date = "0" + t_date;
	if( t_hour.length < 2 ) t_hour = "0" + t_hour;
	if( t_minutes.length < 2 ) t_minutes = "0" + t_minutes;
	
	if( format == CAL_JUSTDATE )
		return t_date + " " + t_month + " " + t_year;
	if( format == CAL_WITHTIME )
		return t_date + " " + t_month + " " + t_year + " " + t_hour + ":" + t_minutes;
		
	return "";
}

function __PopdownCalendar( setback )
{
	if( setback != null )
	{
		if( setback == 1 )
		{
				// Set the input value back to the selected date and time
				_calPopInp.value = __CAL_GetDateString(_selected, _tempStyle);
		}
	}
	__PC_SetCalendarStyles( "_GlobalCalendar" );
	_calPopEl = null;
	_calPopInp = null;
}

function __PC_PopulateTime()
{		
	var hour = "" + _selected.getHours();
	var minutes = "" + _selected.getMinutes();
	if( hour.length < 2 ) hour = "0" + hour;
	if( minutes.length < 2 ) minutes = "0" + minutes;
	
	var gchour = document.getElementById( "_GC_Hour" );
	var gcmin = document.getElementById( "_GC_Minute" );
	
	gchour.innerHTML = hour;
	gcmin.innerHTML = minutes;
}


function __PC_PopulateCalendar()
{
	var gcMonthName = document.getElementById( "_GC_MonthName");
	var gcTodaysDate = document.getElementById( "_GC_TodaysDate");
	
	
	// Set the month title
	monthName = CAL_FULLMONTHS[_tempDate.getMonth()];
	yearName = "" + _tempDate.getFullYear();
	gcMonthName.innerHTML = monthName + " " + yearName;	

	if( _tempStyle == CAL_WITHTIME )
		__PC_PopulateTime();

	var today = new Date();
	gcTodaysDate.innerHTML = today.getDate() + " " + CAL_MONTHS[today.getMonth()] + " " + today.getFullYear();
	
	// Clear all the current cells
	for( var iRow=1; iRow<=6; iRow++ )
	{
		for( var iCol=1; iCol<=7; iCol++ )
		{
			var tdname = "" + iRow + "_" + iCol;
			var cell = document.getElementById(tdname);
			if( cell != null )
			{
				cell.className = "CalendarEmptyCell";
				cell.innerHTML = "&nbsp;";
				cell.dayNumber = 0;
			}
		}
	}
	// Work out if this is inside today's month and year
	var iActiveDay = -1;
	if( (today.getFullYear() == _tempDate.getFullYear()) &&
	    (today.getMonth() == _tempDate.getMonth()))
		iActiveDay = today.getDate();

	var iSelectedDay = -1;
	if((_selected.getFullYear() == _tempDate.getFullYear()) &&
	   (_selected.getMonth() == _tempDate.getMonth()))
	   iSelectedDay = _selected.getDate();

	// Get the first day of the month
	var firstDayMonth = new Date(_tempDate.getFullYear(), _tempDate.getMonth(), 1);
	var firstDay = firstDayMonth.getDay();
	firstDay++;	// 1= Sun, 7=Sat
	// Populate the first row
	var dayCounter = 1;
	var maxDays = CAL_DAYSINMONTH[ _tempDate.getMonth() ];
	if( __PC_ISLEAP( _tempDate.getYear() )) maxDays = CAL_DAYSINMONTHLY[_tempDate.getMonth()];
	
	
	var rowNo = 1;
	var columnNo = firstDay;
	while( (rowNo <= 6) && (dayCounter<=maxDays) )
	{
		while( (columnNo <= 7) && (dayCounter<=maxDays) )
		{
			var tdname = "" + rowNo + "_" + columnNo;
			var cell = document.getElementById(tdname);
			if( cell != null )
			{
				cell.className = "CalendarCell";
				cell.isToday = "no";
				cell.isSelected = "no";
				if( dayCounter == iActiveDay ) 
				{
					cell.className="CalendarCellToday";
					cell.isToday = "yes";
				}
				if( dayCounter == iSelectedDay )
				{
					cell.className = "CalendarCellSelected";
					cell.isSelected = "yes";
				}
				cell.innerHTML = "" + dayCounter;
				cell.dayNumber = dayCounter;
			}
			columnNo++;
			dayCounter++;
		}
		
		rowNo++;
		columnNo=1;
	}
}
function __PC_OverCell(tdobj)
{
	if( tdobj.dayNumber != 0 )
	{
		if( tdobj.isSelected == "yes" )
			tdobj.className = "CalendarCellSelectedOver";
		else if( tdobj.isToday == "yes" )
			tdobj.className = "CalendarCellOverToday";
		else
			tdobj.className = "CalendarCellOver";
	}	
}
function __PC_OutCell(tdobj)
{
	if( tdobj.dayNumber != 0 )
	{
		if( tdobj.isSelected == "yes" )
			tdobj.className = "CalendarCellSelected";
		else if( tdobj.isToday == "yes" )
			tdobj.className = "CalendarCellToday";
		else
			tdobj.className = "CalendarCell";
	}	
}
function __PC_ChooseDate(tdobj)
{
	if( tdobj.dayNumber != 0 )
	{
		_selected.setFullYear(_tempDate.getFullYear());
		_selected.setMonth(_tempDate.getMonth());
		_selected.setDate(tdobj.dayNumber);
		__PopdownCalendar(1);
	}
}
function __PC_ChooseToday()
{
	_tempDate = new Date();
	_selected.setFullYear(_tempDate.getFullYear());
	_selected.setMonth(_tempDate.getMonth());
	_selected.setDate(_tempDate.getDate());
	__PopdownCalendar(1);
}


function __PopupCalendar( el, inp, calstyle )
{
	var inputField = document.getElementById( inp );
	if( inputField == null )
	{
		// Non IE - can't get input elements by Id
		var formObj = document.forms[0];
		if( formObj )
			inputField = eval( "formObj." + inp );
	}

	// Check if we've just clicked same one
	if( _calPopEl != null )
	{
		if( (_calPopEl==el) && (_calPopInp==inputField) ) return __PopdownCalendar();
	}

	// Get the current date setup
	var td = new Date();
	if( inputField != null )
	{
		var millis = Date.parse(inputField.value);
		if( !isNaN(millis))
			td = new Date(millis);
	}
	
	_tempDate = td;
	_selected = new Date(td.getFullYear(), td.getMonth(), td.getDate(), td.getHours(), td.getMinutes());

	__PC_SetCalendarStyles( "_GlobalCalendar" );
	__PC_PopulateCalendar();
	var gcTimePanel = document.getElementById( "_GC_TimePanel" );
	var gcMain = document.getElementById("_GlobalCalendar");
	
	if( calstyle == CAL_WITHTIME )
	{
		// Show the time input panel
		gcTimePanel.style.display = "";		
	}
	else
	{
		gcTimePanel.style.display = "none";
	}
	
	var theInput = document.getElementById( inp );
	if( theInput == null )
	{
		// Non IE - can't get input elements by Id
		var formObj = document.forms[0];
		if( formObj )
			theInput = eval( "formObj." + inp );
	}
	
	gcMain.style.display = "";
	gcMain.style.left = "" + (__CAL_FullOffsetX(theInput)) + "px";
	gcMain.style.top = "" + (__CAL_FullOffsetY(theInput)+20) + "px";
	if( gcMain.focus != null ) gcMain.focus();

	var gcMask = document.getElementById("_GlobalCalendarMask");
	gcMask.style.display = "";
	gcMask.style.left = gcMain.style.left;
	gcMask.style.top = gcMain.style.top;
	gcMask.style.width = gcMain.offsetWidth;
	gcMask.style.height=gcMain.offsetHeight;

	_calPopEl = el;
	_calPopInp = theInput;
	_tempStyle = calstyle;
}
function __PC_OverTimeCtrl( tdobj )
{
	tdobj.className = "TimeControlOver";
}
function __PC_OutTimeCtrl( tdobj )
{
	tdobj.className = "TimeControl";
}
function __PC_DoTime( mode, val )
{
	hour = _selected.getHours();
	mins = _selected.getMinutes();
	
	if( mode == "hour" )
	{
		hour += val;
		while( (hour < 0) || (hour > 23) )
		{
			if( val > 0 ) hour=hour-24;
			else hour=hour+24;
		}
	}
	if( mode == "sethour" )
	{
		hour = val;
	}
	if( mode == "min" )
	{
		mins += val;
		while( (mins < 0) || (mins>59))
		{
			if( val > 0 ) mins=mins-60;
			else mins=mins+60;
		}
	}
	if( mode == "setmin" )
	{
		mins = val;
	}
	
	_selected.setHours(hour);
	_selected.setMinutes(mins);
	__PC_PopulateTime();	
}
